package com.bfsuol.bfsuolframework.security.servlet;

import com.bfsuol.bfsuolframework.core.configuration.ConfigUtils;
import com.bfsuol.bfsuolframework.core.utils.StringUtils;
import com.bfsuol.bfsuolframework.security.PrivilegeService;
import com.bfsuol.bfsuolframework.security.SecurityContext;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "SecurityServlet",urlPatterns = "/servlet/security",loadOnStartup = 10)
public class SecurityServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	static String host=ConfigUtils.get("log.LOG_HOST");
	static String username=ConfigUtils.get("log.username");
	static String password=ConfigUtils.get("log.password");
	private final static ConnectionFactory factory = new ConnectionFactory();
	private static Connection connection=null;
	String to = ConfigUtils.get("mail.errorTo","");
	String cc = ConfigUtils.get("mail.errorCc","");
	String success="1";
	/**
	 * Constructor of the object.
	 */
	public SecurityServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			this.init();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		
		//初始化权限
		PrivilegeService.init();
		//初始化权限管理上下文，合并到同步部分
		String group=ConfigUtils.get("security.SECURITY_SYNC_GROUP");
		if(StringUtils.isBlank(group)){
			SecurityContext.init();
		}else{
			SecurityContext.reload();
		}

	}
}